<?php
/**
 * 计划管理
*/
apf_require_class("Bll_BLL");
apf_require_class("Const_JxStatus");
apf_require_class("Bll_Jx_Relation");
apf_require_class("Bll_Jx_Setting");
class Bll_Jx_Plan extends BLL {


    /**
     * 获取考评完成度的数字
     * @param int  $date 20130103
     */
    public function get_complete_count($date){
        $_where = array(
                'serachMonth'=>$date
                );
        $res = array(
                'no'=>0,
                'yes'=>0
                );
        $data = $this->get_performance_dao()->find($_where,'',0);

        if(!empty($data)){
            $rel_bll = new Bll_Jx_Relation();
            $all_user =  $rel_bll->get_kp_user();
            $uids = array_keys($all_user);
            foreach($data as $val){
                if(!in_array($val['userId'],$uids)){//免考或考评开始后离职
                    continue;
                }
                if($val['status'] < Const_JxStatus::OPT_ACTION_60){
                    $res['no'] +=1;
                }else{
                    $res['yes'] +=1;
                }
            }
        }
        return $res;
    }
    /**
     * 返回已完成或未完成的人
     * @param int $date 20130103
     * @param string $type
     */
    public function get_complete_list($date,$type='yes'){
        //这里需要遍历,从是271提交者开始
        //取所有的用户,有的不用考，有的有计划但已离职
    }

    public function find_performance($where){
        return $this->get_performance_dao()->find($where,'',0);
    }
    /**
     * 取一个员的一期计划
     * @param unknown_type $uid
     * @param unknown_type $date
     * @return multitype:
     */
    public function get_one_performance($uid,$date){
        if(empty($uid) || empty($date)){
            return array();
        }
        return $this->get_performance_dao()->find_row(array('userId'=>$uid,'serachMonth'=>$date));
    }
    /**
     * insert
     * @param int $data
     */
    public function insert_performance($data){
        return $this->get_performance_dao()->insert($data);
    }
    /**
     * update
     * @param int $id
     * @param int $data
     */
    public function update_performance_by_id($id,$data){
        return $this->get_performance_dao()->update_by_id($id,$data);
    }
    /**
     * 删除
     * @param int $performance_id
     */
    public function remove_plan($performance_id,$type=0){
        $_where = array();
        $res = false;
        if($performance_id){
            $_where['user_performance_id'] = $performance_id;
        }
        if($type){
            $_where['type'] = $type;
        }
        if(!empty($_where)){
            $res = $this->get_plan_dao()->remove($_where);
        }
        return $res;
    }
    /**
     * 删除详情
     * @param int $performance_id
     */
    public function remove_plan_detail($performance_id,$type=0){
        $_where = array();
        $res = false;
        if($performance_id){
            $_where['user_performance_id'] = $performance_id;
        }
        if($type){
            $_where['type'] = $type;
        }
        if(!empty($_where)){
            $res = $this->get_plan_view_dao()->remove($_where);
        }
        return $res;
    }
    /**
     * 得到一个员工的一次考评记录
     * @param int $uid
     * @param int $date 20130103 1-3月
     */
    public function get_plan_info($uid,$date){
        $res = array();
        $user_plan = $this->get_performance_dao()->find_row(array('userId'=>$uid,'serachMonth'=>$date));
        if(empty($user_plan)){
            return $res;
        }
        $_where = array('user_performance_id'=>$user_plan['id']);
        $plan_list = $this->get_plan_dao()->find_assoc($_where);
        if(empty($plan_list)){
            return $res;
        }
        $plan_detail = $this->get_plan_view_dao()->find($_where,' id ASC',0);
        $res['base_data'] = $user_plan;
        foreach($plan_detail as $val){
            $res['plan_info'][$val['type']]['base'] = $plan_list[$val['parentId']];
            $res['plan_info'][$val['type']]['detail'][] = $val;
        }
        return $res;
    }
    /**
     * update分类
     * @param int $user_id
     * @param int $date 20130103 1－3月
     * @param int $status
     */
    public function change_status($user_id,$date,$status){
        $_where = array('serachMonth'=>$date);
        if(is_numeric($user_id)){
            $_where['userId']=$user_id;
        }else{
            $_where['userId IN('.implode(',',$user_id).') AND 1=?']=1;
        }
        return $this->get_performance_dao()->update(array('status'=>$status),$_where);
    }
    /**
     * 修改用户参加考评记录，不存在就创建
     * @param int $user_id
     * @param array $data
     * @return boolean
     */
    public function update_user_kp($user_id,$data){
        $check = $this->get_user_kp_dao()->find_row(array('user_id'=>$user_id));
        if(empty($check)){
            $insert = array(
                    'user_id'=>$user_id,
                    'name'=>$data['name'],
                    'superior_code'=>$data['superior_code'],
                    'enter_date'=>$data['enter_date'],
                    'department_id'=>$data['department_id'],
                    'flag'=>0
                    );
            $this->get_user_kp_dao()->insert($insert);
        }else{
            $update = array(
                    'superior_code'=>$data['superior_code'],
                    'department_id'=>$data['department_id'],
                    );
            $this->get_user_kp_dao()->update($update,array('user_id'=>$user_id));
        }
        return true;
    }
    /**-----------Plan-----------*/
    public function batch_insert_plan($data){
        return $this->get_plan_dao()->batch_insert($data);
    }
    public function batch_insert_detail($data){
        return $this->get_plan_view_dao()->batch_insert($data);
    }
    /**
     * 修改update,没有就写入
     */
    public function update_user_plan($data){
        if(empty($data['user_performance_id']) || empty($data['type']) || empty($data['userId'])){
            return false;
        }
        $in_data = array(
                'userId'=>$data['userId'],
                'type'=>$data['type'],
                'percent'=>empty($data['percent']) ? 0 : $data['percent'],
                'personScore'=>empty($data['personScore']) ? 0 : $data['personScore'],
                'treasurerScore'=>empty($data['treasurerScore']) ? 0 : $data['treasurerScore'],
                'creatTime'=>time(),
                'startMonth'=>$data['startMonth'],
                'endMonth'=>$data['endMonth'],
                'serachDate'=>$data['serachDate'],
                'user_performance_id'=>$data['user_performance_id']
                );
        #var_dump($in_data);exit;
        $_where = array(
                'user_performance_id'=>$data['user_performance_id'],
                'userId'=>$data['userId'],
                'type'=>$data['type'],
                );
        $ck = $this->get_plan_dao()->find_row($_where);
        if(empty($ck)){
            $id = $this->get_plan_dao()->insert($in_data);
        }else{
            $id = $ck['id'];
            $this->get_plan_dao()->update_by_id($id,$in_data);
        }
        return $id;
    }
    /**
     * 修改plan_view 没有就写入，否update
     */
    public function update_user_plan_view($data){
        if(empty($data['user_performance_id']) || empty($data['type']) || empty($data['userId'])){
            return false;
        }
        $in_data = array(
                'parentId'=>$data['parentId'],
                'name'=>$data['name'],
                'optionPersent'=>empty($data['optionPersent']) ? 0 : $data['optionPersent'],
                'performanceTarget'=>empty($data['performanceTarget']) ? '' : $data['performanceTarget'],
                'performanceActual'=>empty($data['performanceActual']) ? '' : $data['performanceActual'],
                'userId'=>$data['userId'],
                'serachDate'=>$data['serachDate'],
                'type'=>$data['type'],
                'personScore'=>empty($data['personScore']) ? 0 : $data['personScore'],//自己的打分
                'treasurerScore'=>empty($data['treasurerScore']) ? 0 : $data['treasurerScore'],
                'startMonth'=>$data['startMonth'],
                'endMonth'=>$data['endMonth'],
                'user_performance_id'=>$data['user_performance_id'],
                //'leadernote'=>''
        );
        $_where = array(
                'user_performance_id'=>$data['user_performance_id'],
                'userId'=>$data['userId'],
                'type'=>$data['type'],
        );
        $ck = $this->get_plan_dao()->find_row($_where);
        if(empty($ck)){
            $this->get_plan_view_dao()->insert($in_data);
        }else{
            $this->get_plan_view_dao()->update_by_id($ck['id'],$in_data);
        }
        return true;
    }
    public function update_plan_by_id($id,$data){
        return $this->get_plan_dao()->update_by_id($id,$data);
    }
    public function update_plan_view_by_id($id,$data){
        return $this->get_plan_view_dao()->update_by_id($id,$data);
    }
    /**
     * 统计自己和主管打分总分
     * @param int $p_id
     */
    public function count_self_super_score($p_id){
        $plan_list = $this->get_plan_dao()->find_assoc(array('user_performance_id'=>$p_id));
        $parentids = array_keys($plan_list);
        $_where = array(
                "parentId IN(".implode(',', $parentids).") AND 1=?"=>1
                );
        $detail_list = $this->get_plan_view_dao()->find($_where);

    }
    /**
     * 从考勤获取基本信息
     */
    public function get_user_info_from_kq($user_id){
        $url = APF::get_instance()->get_config('kq_api');
        $user_data = file_get_contents($url.$user_id);
        $user = (array)json_decode($user_data);
        return  $user[0];
    }
    /**
     * 取一个员工的所有下属的考评详情
     * @param string $code
     * @param int $date 20130103
     */
    public function get_sub_user_kp($code,$date){
        $res = array();
        if(empty($code)){
            return $res;
        }
        $rel_bll = new Bll_Jx_Relation();
        //找出这个员工的所有下属,不算免考
        $user_list = $rel_bll->get_lower_user($code,0);
        if(empty($user_list)){
            return $res;
        }
        $uids = array();
        foreach ($user_list as $val){
            $uids[] = $val['user_id'];
        }
        $_where['serachMonth'] = $date;
        $_where['userId IN('.implode(',',$uids).') AND 1=?']=1;
        $kp_info = $this->get_performance_dao()->find($_where,'',0);
        return $kp_info;
    }
    /**
     * 获取员工的历世的考评分,指定年份，就读一年的
     * @param int $user_id
     * @param int $year
     */
    public function get_user_plan_score($user_id,$year=0){
        $res = array();
        $bll = new Bll_Jx_Setting();
        $review_list = $bll->get_performance_month($year);

        if(empty($review_list)){
            return $res;
        }
        rsort($review_list);
        $_where=array(
                "userId"=>$user_id,
                "serachMonth IN(".implode(',',$review_list).") AND 1= ?"=>1
        );
        $data = $this->get_performance_dao()->find($_where,'id DESC',0);
        return array('month_list'=>$review_list,'datalist'=>$data);
    }
    private function get_performance_dao (){
        $dao = $this->get_dao('Dao_jx_UserPerformance');
        return $dao;
    }
    private function get_plan_view_dao (){
        $dao = $this->get_dao('Dao_Jx_UserPlanDetail');
        return $dao;
    }
    private function get_plan_dao (){
        $dao = $this->get_dao('Dao_Jx_UserPlan');
        return $dao;
    }
    private function get_user_kp_dao(){
        $dao = $this->get_dao('Dao_jx_UserKp');
        return $dao;
    }
}
